package com.huawei.esdk.tp.device.smc.v100r002c01.convert;

import java.util.ArrayList;
import java.util.List;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.ScheduleConference;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.AccessCode;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.ArrayOfScheduleSiteParam2;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.ConferenceId;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.DialMode;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.MediaEncryptTypeMapping;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.PresentationTypeMapping;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.ScheduleConf;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.ScheduleConfResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.ScheduleSiteParam2;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.ScheduleTime;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.ScheduledConfInfo;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.ScheduledConfParamEx2;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.SiteFrom;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.SiteType;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.TimeSpanEx;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.VideoFormat;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.VideoParam;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r002c01.VideoProtocol;

public class TPSMC20V1R2C01ConfMgrConvert
{
    public ScheduleConf scheduleConfModel2soap(ScheduleConference conf) throws SDKException
    {
        ScheduleConf scheduleConf = new ScheduleConf();
        
        ScheduledConfParamEx2 scheduledConfParamEx2South = new ScheduledConfParamEx2();
        ConferenceId conferenceId = new ConferenceId();
        Long confId = 0l;
        if (!StringUtils.isEmpty(conf.getId()))
        {
            confId = Long.parseLong(conf.getId());
        }
        conferenceId.setConfInternalId(confId);
        scheduledConfParamEx2South.setId(conferenceId);
        scheduledConfParamEx2South.setName(conf.getName());
        scheduledConfParamEx2South.setAccessCode(conf.getAccessCode());

        ScheduleTime scheduleTimeSouth = new ScheduleTime();
        // conftime-BeginTime
        if (null != conf.getBeginTime())
        {
            scheduleTimeSouth.setBeginTime(DateUtils.dateToString(conf
                .getBeginTime()));
        }
        TimeSpanEx timeSpanExSouth = new TimeSpanEx();
        // conftime-Duration
        timeSpanExSouth.setTimeSpan("PT" + conf.getLength() + "M");
        scheduleTimeSouth.setDuration(timeSpanExSouth);
        // conftime-Recurrence-周期性会议的情况，不考虑
        scheduledConfParamEx2South.setConfTime(scheduleTimeSouth);
        // 速率rate放在ScheduleConference中
        scheduledConfParamEx2South.setRate(conf.getRate());
        scheduledConfParamEx2South.setBillCode(conf.getBillCode());
        scheduledConfParamEx2South.setPassword(conf.getPassword());
        //cpResource是int类型，需要判断是否为null
        if (null != conf.getCpResouce())
        {
            scheduledConfParamEx2South.setCpResource(conf.getCpResouce()); 
        }
        scheduledConfParamEx2South.setMediaEncryptType(MediaEncryptTypeMapping
                .int2Enum(conf.getMediaEncryptType()));
        scheduledConfParamEx2South.setPresentation(PresentationTypeMapping
                .int2Enum(conf.getPresentation()));

        if (null != conf.getIsLiveBroadcast())
        {
            scheduledConfParamEx2South.setIsLiveBroadcast(0 == conf
                    .getIsLiveBroadcast() ? false : true);
        }
        if (null != conf.getIsRecording())
        {
            scheduledConfParamEx2South.setIsRecording(0 == conf
                    .getIsRecording() ? false : true);
        }

        scheduleConf.setNewConf(scheduledConfParamEx2South);

        ArrayOfScheduleSiteParam2 arrayOfScheduleSiteParam2South = new ArrayOfScheduleSiteParam2();
        ScheduleSiteParam2 scheduleSiteParam2South = null;
        List<SiteInConference> siteInConferences = conf.getSites();

        // 会议中的会场数据
        for (SiteInConference siteInConference : siteInConferences)
        {
            if (null != siteInConference)
            {
                scheduleSiteParam2South = siteCommModel2soap(siteInConference);
                arrayOfScheduleSiteParam2South.getScheduleSiteParam2().add(
                        scheduleSiteParam2South);
            }
        }

        scheduleConf.setSites(arrayOfScheduleSiteParam2South);
        return scheduleConf;
    }
    
    /**
     * 会场信息转换 转换时按照对象的粒度转换 南向领域bean转南向soap bean 用于向SMC产品发消息的情况 response
     * model2soap
     * @throws SDKException 
     */
    private ScheduleSiteParam2 siteCommModel2soap(
            SiteInConference siteInConference) throws SDKException
    {
        ScheduleSiteParam2 scheduleSiteParam2South = new ScheduleSiteParam2();
        scheduleSiteParam2South.setUri(siteInConference.getUri());
        scheduleSiteParam2South.setName(siteInConference.getName());

        if (!StringUtils.isEmpty(siteInConference.getSiteType()))
        {
            scheduleSiteParam2South.setType(SiteType.valueOf(siteInConference
                    .getSiteType()));
        }
        if (!StringUtils.isEmpty(siteInConference.getFrom()))
        {
            scheduleSiteParam2South.setFrom(SiteFrom.valueOf(siteInConference
                    .getFrom()));
        }
        if (!StringUtils.isEmpty(siteInConference.getDialingMode()))
        {
            scheduleSiteParam2South.setDialingMode(DialMode
                    .valueOf(siteInConference.getDialingMode()));
        }
        scheduleSiteParam2South.setRate(siteInConference.getRate());
        VideoParam videoParamSouth = new VideoParam();


        videoParamSouth.setFormat(getVideoFormatInt2Enum(siteInConference
                .getVideoFormat()));

        videoParamSouth.setProtocol(getVideoProtocolInt2Enum(siteInConference
                .getVideoProtocol()));

        if(null == siteInConference.getVideoProtocol() && null == siteInConference.getVideoFormat())
        {
            scheduleSiteParam2South.setVideo(null);
        }
        else 
        {
            scheduleSiteParam2South.setVideo(videoParamSouth);
        }

        return scheduleSiteParam2South;
    }
    public SDKResult<ScheduleConference> scheduleConfSoap2model( ScheduleConfResponse responseSouth)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        long errorCode = responseSouth.getScheduleConfResult().getErrCode();
        result.setErrCode(responseSouth.getScheduleConfResult().getErrCode());

        if (0 != errorCode)
        {
            return result;
        }
        ScheduleConference desMsg = new ScheduleConference();

        setConferenceEx(responseSouth, desMsg);
        result.setResult(desMsg);

        return result;
    }
    
    /**
     * 预约会议 南向soapbean转南向领域bean 用于返回值的情况 ModelBean soap2model(SoapBean){}
     */
    private void setConferenceEx(ScheduleConfResponse resMsg,
            ScheduleConference desMsg)
    {
        ScheduledConfInfo scheduledConfInfoSouth = resMsg.getConfs()
                .getScheduledConfInfo().get(0);
        desMsg.setId(scheduledConfInfoSouth.getId().getConfInternalId()
                .toString());
        List<AccessCode> arrayOfAccessCodeSouth = scheduledConfInfoSouth
                .getAccessCode().getAccessCode();
        List<SiteInConference> siteInConferences = new ArrayList<SiteInConference>();
        SiteInConference siteInConference = null;
        for (AccessCode accessCodeSouth : arrayOfAccessCodeSouth)
        {
            siteInConference = siteCommSoap2model(accessCodeSouth);
            siteInConferences.add(siteInConference);
        }
        desMsg.setSites(siteInConferences);
        
        //Add in R3
        desMsg.setRecorderAddr(scheduledConfInfoSouth.getRecorderAddr());
    }
    
    /**
     * 预约会议中会场信息的转换 南向soapbean转南向领域bean 用于返回值的情况 ModelBean
     * soap2model(SoapBean){}
     */
    private SiteInConference siteCommSoap2model(AccessCode accessCodeSouth)
    {

        SiteInConference siteInConference = new SiteInConference();
        siteInConference.setUri(accessCodeSouth.getSiteUri());
        siteInConference.setName(accessCodeSouth.getSiteName());
        siteInConference.setAccessCode(accessCodeSouth.getConfAccessCode());
        siteInConference.setBeginTime(DateUtils.toDate(accessCodeSouth
                .getBeginTime()));

        return siteInConference;
    }
    
    private VideoFormat getVideoFormatInt2Enum(Integer num) 
        throws SDKException
    {
        final int CIF_4 = 0;
        final int P_720 = 1;
        final int P_1080 = 2;
        final int P_480 = 3;
        final int P_720_60_F = 4;
        final int P_1080_60_F = 5;
        final int Q_CIF= 6;
        final int CIF= 7;
        final int SQ_CIF= 8;
        final int CIF_2= 9;
        final int I_108= 10;
        final int I_480= 11;
        final int MIN = 0;
        final int MAX = 11;

        if(null==num){
            return null;
        }
        
        if(num < MIN || num > MAX)
        {
            SDKException sdkException = new SDKException(ErrInfo.DATA_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.DATA_ERRORCODE);
            throw sdkException;
        }
        
        switch (num) {
            case CIF_4: {
                return VideoFormat.CIF_4;
            }
            case P_720: {
                return VideoFormat.P_720;
            }
            case P_1080: {
                return VideoFormat.P_1080;
            }
            case P_480: {
                return VideoFormat.P_480;
            }
            case P_720_60_F: {
                return VideoFormat.P_720_60_F;
            }
            case P_1080_60_F: {
                return VideoFormat.P_1080_60_F;
            }
            case Q_CIF: {
                return VideoFormat.Q_CIF;
            }
            case CIF: {
                return VideoFormat.CIF;
            }
            case SQ_CIF: {
                return VideoFormat.SQ_CIF;
            }
            case CIF_2: {
                return VideoFormat.CIF_2;
            }
            case I_108: {
                return VideoFormat.I_108;
            }
            case I_480: {
                return VideoFormat.I_480;
            }
            default: {
                return null;
            }
        }
    }
    
    private VideoProtocol getVideoProtocolInt2Enum(Integer num) 
        throws SDKException
    {
        final int H_261 = 0;
        final int H_263 = 1;
        final int H_264 = 2;
        final int MIN = 0;
        final int MAX = 2;

        if(null==num){
            return null;
        }
        
        if(num < MIN || num > MAX)
        {
            SDKException sdkException = new SDKException(ErrInfo.DATA_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.DATA_ERRORCODE);
            throw sdkException;
        }
        
        switch (num) 
        {
            case H_261: {
                return VideoProtocol.H_261;
            }
            case H_263: {
                return VideoProtocol.H_263;
            }
            case H_264: {
                return VideoProtocol.H_264;
            }
            default: {
                return null;
            }
        }
    }
}

